<?php

namespace App\Admin\Controllers;

use App\Admin\Selectable\BusinessTimes;
use App\Admin\Selectable\Scales;
use App\Models\Goods;
use App\Models\Scale;
use App\Models\SetMeal;
use App\Models\BusinessTime;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Auth;

class SetMealController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '套餐';


    public function __construct()
    {
        $this->type = ['0'=>'请选择',1=>'清唱',2=>'套餐',3=>'最低消费'];
    }



    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new SetMeal());

        $grid->number('序号');
        $grid->rows(function ($row, $number) {
            $row->column('number', $number+1);
        });
        $grid->column('type', __('类型'))
            ->using($this->type);
        $grid->column('scale.name', __('房型'));
        $grid->column('business_time_id', __('时间段'))->display(function ($id){
            if (!empty($id)){
                $find = BusinessTime::find($id);
                if ($find){
                    $start = BusinessTime::find($id)->start;
                    $end = BusinessTime::find($id)->end;
                    if ($end>=86400-8*3600){
                        return date('H:i:s',$start).'-'.'次日'.date('H:i',$end);
                    }
                    return date('H:i:s',$start).'-'.date('H:i:s',$end);
                }

            }
        });
        $grid->column('name', __('套餐名称'));
        $grid->column('images_url', __('封面'))->image('', '40');
        $grid->column('package_price', __('套餐价格'))->display(function ($price){
            return '￥'.$price;
        })->sortable();
        $grid->column('price', __('价格'))->display(function ($price){
            return '￥'.$price;
        })->sortable();
        $grid->column('current_price', __('清唱时价'))->display(function ($price){
            return '￥'.$price;
        })->sortable();
        $grid->column('minimum_price', __('最低消费金额'))->display(function ($price){
            return '￥'.$price;
        })->sortable();
        $grid->column('deposit_price', __('最低消费定金'))->display(function ($price){
            return '￥'.$price;
        })->sortable();
		// $grid->column('number_price', __('人数消费单价'))->sortable();
        $grid->column('status', __('状态'))
            ->switch([
                'on' => ['value' => 1 , 'text' => '正常'],
                'off' => ['value' => 2, 'text' => '禁用']
            ]);
        $grid->column('created_at', __('创建时间'))->sortable();

        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->like('name', '套餐名称');
            $filter->equal('status','状态')->radio([
                ''   => '全部',
                1    => '正常',
                2    => '禁用',
            ]);
            $filter->equal('type','类型')->select($this->type);
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(SetMeal::findOrFail($id));

        $show->field('type', __('类型'))->using($this->type);
        $show->field('name', __('套餐名称'));
        $show->field('describe', __('套餐描述'));
        $show->field('images_url', __('封面'))->image();
        $show->field('price', __('套餐价格'));
        $show->field('time', __('套餐时长'));
        $show->field('status', __('状态'))->using(['1'=>'正常',2=>'禁用']);
        $show->field('created_at', __('创建时间'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new SetMeal());

        $form->hidden('store_id', __('门店id'));
        $form->select('type', __('类型'))
            ->options($this->type)
            ->when(1,function (Form $form){
                $form->currency('current_price', __('Current_price'))
                    ->symbol('￥')->required();
            })
            ->when(2,function (Form $form){
                $form->currency('package_price', __('Package_price'))
                    ->symbol('￥')->required();
            })
            ->when(3, function (Form $form) {
                $form->currency('minimum_price', __('Minimum_price'))
                    ->symbol('￥')->required();
                $form->currency('deposit_price', __('Deposit_price'))
                    ->symbol('￥')->required();
            })->when(4, function (Form $form) {
                $form->currency('number_price', __('Number_price'))
                    ->symbol('￥')->required();
            })
            ->default(0);

        $form->text('name', __('套餐名称'))->required();
        $form->hidden('price', __('价格'));

        $form->text('describe', __('套餐描述'))->required();

//        $form->belongsTo('scale_id', Scales::class, '房型')->required();
        $form->select('scale_id', '房型')->options(
            Scale::where('store_id', Auth::guard('admin')->user()->store_id)->get()->pluck('name', 'id')
        )->required();

        $form->belongsTo('business_time_id', BusinessTimes::class, '时间段')->required();

        $form->hasMany('goods','选择商品', function (Form\NestedForm $form) {
            $form->select('goods_id',__('商品'))
                ->options(Goods::where('store_id', Admin::user()->store_id)->pluck('name', 'id'));
            $form->text('number',__('数量'));
        });

        $form->image('images_url', __('封面'))
            ->removable()
            ->uniqueName()
            ->required();
        $form->switch('status','状态')
            ->states([
                'on' => ['value' => 1 , 'text' => '正常', 'color' => 'success'],
                'off' => ['value' => 2, 'text' => '禁用', 'color' => 'success']
            ])
            ->default(1);

        $form->saving(function (Form $form) {
            $form->store_id = Auth::guard('admin')->user()->store_id;
            switch ($form->type) {
                case 1:
                    $form->minimum_price = "";
                    $form->deposit_price = "";
                    $form->number_price = "";
                    $form->package_price = "";
                    $start = BusinessTime::find($form->business_time_id)->start;
                    $end = BusinessTime::find($form->business_time_id)->end;
                    $time = ($end - $start) / 3600;
                    $form->current_price = $form->current_price * 100;
                    $form->price = $time * $form->current_price; //分
                    break;
                case 2:
                    $form->minimum_price = "";
                    $form->deposit_price = "";
                    $form->number_price = "";
                    $form->current_price = "";
                    $form->package_price = $form->package_price*100;
                    $form->price = $form->package_price;
                    break;
                case 3:
                    $form->number_price = "";
                    $form->current_price = "";
                    $form->package_price = "";
                    $form->minimum_price = $form->minimum_price*100;
                    $form->deposit_price = $form->deposit_price*100;
                    $form->price = $form->deposit_price;
                    break;
                default:
                    $form->minimum_price = "";
                    $form->deposit_price = "";
                    $form->current_price = "";
                    $form->package_price = "";
                    $form->number_price = $form->number_price*100;

            }
            //dd($time, $form);
        });

        return $form;
    }
}
